﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.Common;

namespace SqlOrm
{
    public static class Extensions
    {
        public static string F(this string instance, params object[] obj)
        {
            return string.Format(instance, obj);
        }
        public static string GetResourceTemplate(this Assembly instance, string template)
        {
            using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(template)))))
                return sr.ReadToEnd();
        }
        public static string SepList<T>(this IEnumerable<T> items, Func<T, string> expression, Func<T, string> seperator, bool complete = true)
        {
            var sb = new StringBuilder();
            foreach (var item in items)
            {
                sb.Append(expression(item));
                if (Array.IndexOf(items.ToArray(), item) != items.Count() - 1 || !complete)
                    sb.Append(seperator(item));
            }
            return sb.ToString();
        }
        public static void AddWithValue(this DbCommand cmd, string name, object val)
        {
            DbParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.Value = val;
            cmd.Parameters.Add(param);
        }
    }
}
